package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Vo.Reviewer;
import com.example.entity.Position;
import com.example.mapper.PositionMapper;
import com.example.service.IPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ptuDai
 * @since 2022-04-16
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Autowired
    PositionMapper positionMapper;

    @Override
    public List<Position> tree() {
        List<Position> positions = buildTreePosition(this.list(new QueryWrapper<Position>().orderByAsc("create_time")));
        return positions;
    }

    @Override
    public List<Reviewer> listReviewer(Integer level) {
        return positionMapper.listReviewer(level,null);
    }

    @Override
    public List<Reviewer> listReviewer(Integer level,Integer departmentId) {
        return positionMapper.listReviewer(level,departmentId);
    }

    private List<Position> buildTreePosition(List<Position> positions) {
        List<Position> finalPositions = new ArrayList<>();
        for (Position position : positions) {
            // 先寻找各自的孩子
            for (Position e : positions) {
                if (e.getParentLevel().equals(position.getLevel())) {
                    position.getChildren().add(e);
                }
            }
            // 提取出父节点
            if (position.getParentLevel() == 0) {
                finalPositions.add(position);
            }
        }
        return finalPositions;
    }
}
